package com.example.demo.config;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;


/**
 * TODO
 *
 * @author caiyonglan
 * @email 627064518@qq.com
 * @date 2020/12/2 17:32
 */
@Component
public class MyHandlerInterceptor implements HandlerInterceptor {

//    /**
//     * 控制器方法被调用之前
//     *
//     * @param httpServletRequest
//     * @param httpServletResponse
//     * @param handler
//     * @return true表示请求放行，false表示拦截请求，不再向下寻找处理器
//     * @throws Exception
//     */
//    @Override
//
//    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler) throws Exception {
//        // 设置响应参数
//        httpServletResponse.setCharacterEncoding("UTF-8");
//        httpServletResponse.setContentType("application/json; charset=utf-8");
//        httpServletResponse.setCharacterEncoding("utf-8");
//
//        // API请求不合法
//        if (!handler.getClass().isAssignableFrom(HandlerMethod.class)) {
////            JSONObject json = new JSONObject();
////            json.put("msg", "API '" + httpServletRequest.getRequestURI() + "' is error");
////            json.put("status", 405);
////            httpServletResponse.setStatus(405);
////            httpServletResponse.getWriter().append(json.toJSONString());
////            System.out.println("[API ERROR]======API '" + httpServletRequest.getRequestURI() + "' is error, " + "cat cast handler to HandlerMethod.class");
//            return false;
//        }
//
//        // HTTP 的 OPTIONS 方法 用于获取目的资源所支持的通信选项。
//        if ("OPTIONS".equals(httpServletRequest.getMethod())) {
//            httpServletResponse.setStatus(HttpServletResponse.SC_OK);
//            return true;
//        }
//
//        // API合法后拦截请求
////        // 获取控制器的名字
////        String controllerName = ((HandlerMethod) handler).getBean().getClass().getName();
////        String method = httpServletRequest.getMethod();
////        String api = httpServletRequest.getRequestURI();
////        // 获取方法名
////        String controllerMethod = ((HandlerMethod) handler).getMethod().getName();
////        String arg = httpServletRequest.getQueryString();
//
////        System.out.println("[preHandle]========" + controllerName + "========[---Method: " + method + "---   API: " + api + "    Arguments: " + arg + "  ----]     ControllerMethod: " + controllerMethod);
//
//        return true;
////        // 获取API头部信息
////        String token = httpServletRequest.getHeader("Authorization");
////        if (token != null) {
////            boolean result = TokenUtil.verify(token);
////            if (result) {
////                // token验证成功
////                return true;
////            }
////        }
////        // token验证失败
////        JSONObject json = new JSONObject();
////        json.put("msg", "token验证失败");
////        json.put("status", 401);
////        httpServletResponse.setStatus(401);
////        httpServletResponse.getWriter().append(json.toJSONString());
////        return false;
//    }

//    /**
//     * 控制器的方法处理之后 如果出现异常则不调用
//     * @param httpServletRequest
//     * @param httpServletResponse
//     * @param o
//     * @param modelAndView
//     * @throws Exception
//     */
//    @Override
//    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
//
//        // 获取控制器的名字
//        String controllerName = ((HandlerMethod) o).getBean().getClass().getName();
//        // 获取方法名
//        String controllerMethod = ((HandlerMethod) o).getMethod().getName();
//
//        System.out.println("[postHandle]========" + controllerName + "========[---Method---]     " + controllerMethod);
//
//    }
//
//    /**
//     *  出不出现异常都会调用
//     * @param httpServletRequest
//     * @param httpServletResponse
//     * @param o
//     * @param e
//     * @throws Exception
//     */
//    @Override
//    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
//
//        if ( e != null) {
//            System.out.println("[afterCompletion]========[---Exception---]========");
//            System.out.println(e.getStackTrace());
//        }
//    }
}
